﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeliciousTransform
{
    class Bookmark
    {
        // Fully preserved.
        public string URL;

        // Fully preserved.
        public string Description;

        // Preserved with substitutions.
        public string[] Tags;

        // Preserved with substitutions.
        public string Name;

        // Fully preserved.
        public string AddDate;

        // Thrown out. This value could be stored in the Notes field of the URL file if desired.
        public string Private;

        // Another useful data preservation technique would be to capture the full original
        // lines from the Delicious exported Netscape Bookmark Format file as text in the Notes
        // field of the .url file.

        public void Print()
        {
            Console.WriteLine("        URL: " + URL);
            Console.WriteLine("   Add Date: " + AddDate);
            Console.WriteLine("    Private: " + Private);
            Console.WriteLine("       Tags: ");
            int i = 1;
            foreach (string tag in Tags)
            {
                Console.WriteLine("         [" + i++ + "] " + tag);
            }
            Console.WriteLine("       Name: " + Name);
            Console.WriteLine("Description: " + Description);

            return;
        }

        public void Save()
        {
            string root = @"C:\Favorites";

            foreach (string tag in Tags)
            {
                string dir = root + "\\" + tag;
                System.IO.Directory.CreateDirectory(dir);

                string filename = dir + "\\" + Name + ".url";
                System.IO.StreamWriter file = new System.IO.StreamWriter(filename);

                // URL
                file.WriteLine("[DEFAULT]");
                file.WriteLine("BASEURL=" + URL);
                file.WriteLine("[InternetShortcut]");
                file.WriteLine("URL=" + URL);

                // Description
                // for details see
                // http://support.microsoft.com/kb/2568750
                file.WriteLine("[{5CBF2787-48CF-4208-B90E-EE5E5D420294}]");
                file.Write("Prop21=31,");
                file.WriteLine(Description);

                file.Close();

                // Add date = file creation time.
                DateTime filecreate = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                filecreate = filecreate.AddSeconds(Convert.ToDouble(AddDate));
                System.IO.FileInfo fileprops = new System.IO.FileInfo(filename);
                fileprops.CreationTime = filecreate;
            }

            return;
        }
    }
}
